package www.my.imageframecompare;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import java.util.List;

/**
 * Created by ${张英杰} on 2016/12/28.
 */

public class MainAdapter extends RecyclerView.Adapter {
    List<MainActivity.TempSave> mlist;

    public MainAdapter(List<MainActivity.TempSave> mlist) {
        this.mlist = mlist;
    }

    public void setData(List<MainActivity.TempSave> mlist) {
        this.mlist = mlist;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_mainadapter,
                viewGroup, false);
        return new RecyclerView.ViewHolder(view) {
            @Override
            public String toString() {
                return super.toString();
            }
        };
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        View view = holder.itemView;
        Button btn = (Button) view.findViewById(R.id.btn);
        btn.setText(mlist.get(position).name);
        //手动更改高度，不同位置的高度有所不同
//        btn.setHeight(100 + (position % 3) * 100);
        if (adapterInterface != null) {
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    adapterInterface.itemClickListener(position);
                }
            });
        }
    }


    @Override
    public int getItemCount() {
        return mlist != null ? mlist.size() : 0;
    }

    public interface ItemClickListener {
        void itemClickListener(int position);
    }

    private ItemClickListener adapterInterface;

    public void setOnItemClickListener(ItemClickListener inter) {
        adapterInterface = inter;
    }
}
